package com.gitee.wsl.jvm.ext.base

/*import org.json.JSONArray
import org.json.JSONObject
import timber.log.Timber
import java.util.Locale


object JSONArrayExt{

    *//**
     * 字符串 JSON 转义处理
     * @param str 字符串
     * @return 转义后的 JSON 字符串
     *//*
    fun stringJSONEscape(str: String): String {
        val builder = StringBuilder()
        var i = 0
        val len = str.length
        while (i < len) {
            when (val ch = str[i]) {
                '"' -> builder.append("\\\"")
                '\\' -> builder.append("\\\\")
                '\b' -> builder.append("\\b")
                '\u000c' -> builder.append("\\u000c")
                '\n' -> builder.append("\\n")
                '\r' -> builder.append("\\r")
                '\t' -> builder.append("\\t")
                '/' -> builder.append("\\/")
                else -> if (ch <= '\u001F') {
                    val ss = Integer.toHexString(ch.code)
                    builder.append("\\u")
                    var k = 0
                    while (k < 4 - ss.length) {
                        builder.append('0')
                        k++
                    }
                    builder.append(ss.uppercase(Locale.getDefault()))
                } else {
                    builder.append(ch)
                }
            }
            i++
        }
        return builder.toString()
    }

    *//**
     * 将 JSON 对象转化为 Map
     * @param jsonObject [JSONObject]
     * @return [Map]
     *//*
    fun jsonToMap(jsonObject: JSONObject): Map<String, Any>? {
        if (JSONObject.NULL == jsonObject) return null
        try {
            val map: MutableMap<String, Any> = LinkedHashMap()
            val iterator = jsonObject.keys()
            while (iterator.hasNext()) {
                val key = iterator.next()
                var value = jsonObject[key]
                if (value is JSONArray) {
                     jsonToList(value)?.let { value = it }
                } else if (value is JSONObject) {
                     jsonToMap(value as JSONObject)?.let{ value = it }
                }
                map[key] = value
            }
            return map
        } catch (e: Exception) {
           Timber.d( e, "jsonToMap")
        }
        return null
    }

    *//**
     * 将 JSON 对象转化为 List
     * @param jsonArray [JSONArray]
     * @return [List]
     *//*
    fun jsonToList(jsonArray: JSONArray): List<Any>? {
        if (JSONObject.NULL == jsonArray) return null
        try {
            val list: MutableList<Any> = ArrayList()
            var i = 0
            val len = jsonArray.length()
            while (i < len) {
                var value = jsonArray[i]
                if (value is JSONArray) {
                    value = jsonToList(value)!!
                } else if (value is JSONObject) {
                    value = jsonToMap(value)!!
                }
                list.add(value)
                i++
            }
            return list
        } catch (e: java.lang.Exception) {
            Timber.d( e, "jsonToList")
        }
        return null
    }

}

fun JSONObject.getObj(value: String): JSONObject {
    return this.getJSONObject(value)
}

fun JSONObject.getArray(value: String): JSONArray {
    return this.getJSONArray(value)
}

inline fun <reified T> JSONArray.getOrNull(index: Int): T? {
    return if (index > 0 && index < this.length() - 1) {
        this.getJSONObject(index) as T
    } else {
        null
    }
}

inline fun JSONArray.iterator(): Iterator<JSONObject> =
    (0 until length()).asSequence().map { get(it) as JSONObject }.iterator()

inline fun <reified T> JSONArray.forEach(action: (T?) -> Unit) {
    (0 until length()).forEach { action(get(it) as? T) }
}

fun JSONArray.isEmpty() = this.length() == 0

fun JSONArray.isNotEmpty() = isEmpty().not()

fun JSONArray.first(): JSONObject = getJSONObject(0)

fun JSONArray.hasAny(args: List<String>): Boolean =
    (0 until length()).asSequence().map { getString(it) }.any {
        args.contains(it)
    }

fun JSONArray.hasAll(args: List<String>): Boolean =
    (0 until length()).asSequence().map { getString(it) }.all {
        args.contains(it)
    }*/
